﻿
-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Ali Balci>
-- Create date: <27/03/2011>
-- Description:	<sp_UserProfile_Delete>
-- =============================================
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'sp_UserProfile_Delete')
	BEGIN
		DROP  Procedure  sp_UserProfile_Delete
	END

GO

CREATE PROCEDURE sp_UserProfile_Delete
	-- Add the parameters for the stored procedure here
	(
				@UserKey int,

				@Sys_ModifiedByID varchar(50) = NULL,
				@Sys_DeleteType int = 0,
				@Sys_OrganizationID int = -1,
				@Sys_InitialPrimaryKeys varchar(100) = NULL,
				@Sys_WorkstationName varchar(50) = NULL,
				@ConcurrencyTimeStamp datetime  = NULL,

				@Sys_IsDeleted bit OUTPUT,
				@Sys_DateOfModification datetime OUTPUT
	)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	IF @ConcurrencyTimeStamp = NULL
	BEGIN
		RAISERROR('Missing @ConcurrencyTimeStamp', 11,1);
	END
	DECLARE @CurrentModificationDate datetime;
	SET @CurrentModificationDate = (SELECT [Sys_DateOfModification] FROM [User] WHERE [UserKey] = @UserKey);
	
	If @ConcurrencyTimeStamp != @CurrentModificationDate
	BEGIN
		RAISERROR('Record is Changed by another process', 11,1);
	END
	
	IF @Sys_WorkstationName = 'Default'	
	BEGIN
		SET @Sys_WorkstationName = (SELECT @@SERVERNAME);
	END
	SET @Sys_DateOfModification = GETDATE();
	
	UPDATE	[UserProfile]
	SET 
			[Sys_DateOfModification] = @Sys_DateOfModification, 
			[Sys_ModifiedByID] = @Sys_ModifiedByID, 
			[Sys_DeleteType] = @Sys_DeleteType, 
			[Sys_IsDeleted] = 1,
			[Sys_OrganizationID] = @Sys_OrganizationID,
			[Sys_InitialPrimaryKeys] = @Sys_InitialPrimaryKeys, 
			[Sys_WorkstationName] = @Sys_WorkstationName 
	WHERE 
		(
			[UserKey] = @UserKey
		)
 
END
GO
